import org.w3c.dom.ls.LSOutput;

import java.lang.reflect.Array;
import java.util.Arrays;

public class test {

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        //将-1赋给array[2]--array[5-1]
        Arrays.fill(array,2,5,-1);
        System.out.println(Arrays.toString(array));

        //将-1赋给array每个元素
        Arrays.fill(array,-1);
        System.out.println(Arrays.toString(array));
    }
    public static void main9(String[] args) {
        int[] array = {1,2,3,4};
        int[] array2 = {1,2,3,4};
        boolean flag = Arrays.equals(array,array2);
        System.out.println(flag);
    }

    public static void main8(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{};
        int[][] array3 = new int[3][];

        for (int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[0].length; j++){
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("===============");

        for (int[] x:array) {
            for(int a : x){
                System.out.print(a+" ");
            }
            System.out.println();
        }

        System.out.println("===============");

        System.out.println(Arrays.deepToString(array));

    }

    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] array2;
        array2 = new int[]{};
        array2 = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(array2));

        System.arraycopy(array,0,array2,0,array.length);
        System.out.println(Arrays.toString(array2));


        array2 = Arrays.copyOfRange(array,1,5);
        for(int i = 0; i<array.length; i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
        System.out.println(Arrays.toString(array2));

        System.out.println(Arrays.binarySearch(array, 4));

        int[] array3 = {2,5,8,3,7,4};
        Arrays.sort(array3);
        System.out.println(Arrays.toString(array3));
    }

//    给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。

    public static int[] arraySum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
//        return new int[]{};
        return null;
    }

    public static void main6(String[] args) {
        int[] nums = {2,7,11,15};
        int[] ret = arraySum(nums, 9);
        if(ret == null){
            System.out.println("找不到可以凑成目标值和的数");
        }else{
            System.out.println(Arrays.toString(ret));
        }
    }
//    给定一个整型数组, 实现冒泡排序(升序排序)
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for(int j = 0; j < array.length - 1 - i; j++){
                if(array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }

    public static void main5(String[] args) {
        int[] array = {4,5,7,1,2,8,9,3,6,0};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
//    给定一个有序整型数组, 实现二分查找
    public static int binarySearch(int array[],int n){
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(array[mid] == n){
                return mid;
            }else if(array[mid] > n){
                right = mid  - 1;
            }else{
                left = mid + 1;
            }
        }
        return -1;
    }
    public static void main4(String[] args) {
        int[] array = {1,3,5,7,9,11};
        int ret = binarySearch(array,9);
        if(ret != -1){
            System.out.println("查找的数字在数组元素的下标为："+ret);
        }else{
            System.out.println("查找的数字在数组元素中不存在");
        }
    }
    
//    调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
//    如数组：[1,2,3,4,5,6]
//    调整后可能是：[1, 5, 3, 4, 2, 6]
    public static void swap(int[] array,int len){
        int left = 0;
        int right = len - 1;
            while (left < right) {
                while(left < right && array[left] % 2 != 0) {
                    left++;
                }

                while(left < right && array[right] % 2 == 0){
                    right--;
                }

                if(left < right){
                    int tmp = array[left];
                    array[left] = array[right];
                    array[right] = tmp;
                    left++;
                    right--;
                }
            }
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int len = array.length;
        swap(array,len);
        System.out.println(Arrays.toString(array));
    }

    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
    public static int transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
        return 0;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }

    public static void main1(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5};

        array1 = new int[]{1,2,3,4,5};

        int[] array2 = {1,2,3,4,5};

        int[] array;
        array = new int[]{1,2,3,4,5};

        //第一种打印方式
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();

        //第二种打印方式
        for(int x : array){
            System.out.print(x+" ");
        }
        System.out.println();

        //第三种打印方式
        String array3 = Arrays.toString(array);
        String str = Arrays.toString(array);
        System.out.println(array3);
        System.out.println(Arrays.toString(array));


    }
}
